package com.atguigu.spzx.manager.task;

import cn.hutool.core.date.DateUtil;
import com.atguigu.spzx.manager.mapper.OrderInfoMapper;
import com.atguigu.spzx.manager.mapper.OrderStatisticsMapper;
import com.atguigu.spzx.model.entity.order.OrderStatistics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 作者:hfj
 * 功能:
 * 日期: 2024/3/30 9:39
 */
@Component
public class OrderStatisticsTask {

    //定义定时任务，使用@Scheduled注解指定调度时间表达式
    //@Scheduled(cron = "0/5 * * * * ?")
    //public void test(){
    //    System.out.println(new Date().toInstant());
    //}

   @Autowired
   private OrderInfoMapper orderInfoMapper;

   @Autowired
    private OrderStatisticsMapper orderStatisticsMapper;

    /**
     * 每天凌晨2点,查询前一天日期统计数据,把统计之后数据添加统计结果表里面
     */
    @Scheduled(cron = "0 0 2 * * ?")
    public void orderTotalAmountStatistics() {
        //1.获取前一天的日期
        String createTime = DateUtil.offsetDay(new Date(), -1).toString(new SimpleDateFormat("yyyy-MM-dd"));

        //2.根据前一天日期进行统计功能,(分组操作) 统计前一天的交易金额
        OrderStatistics orderStatistics = orderInfoMapper.selectOrderStatistics(createTime);

        //3.把统计之后的数据,添加统计结果表里面
        if(orderStatistics != null) {
            orderStatisticsMapper.insert(orderStatistics);
        }
    }
}
